#include "game_sample.h"

void game_sample_setup(Game *game)
{
    game->logic = game_sample_logic;
    game->collide_entity =  game_sample_collide_entity;
    game->collide_tilemap =  game_sample_collide_tilemap;
    game->draw_player_gui = game_sample_draw_player_gui;

    game->tilemap = gfx_tilemap_create("gfx_map_random", 20, 10);
    gfx_tilemap_set_tiles(game->tilemap, 0, 0,    game->tilemap->width, 1, 1);
    gfx_tilemap_set_tiles(game->tilemap, 0, game->tilemap->height-1,    game->tilemap->width, 1, 1);
    gfx_tilemap_set_tiles(game->tilemap, 0, 0,                          1, game->tilemap->height, 1);
    gfx_tilemap_set_tiles(game->tilemap, game->tilemap->width-1, 0,      1, game->tilemap->height, 1);

    for(int i = 0; i < array_size(game->localPlayers); i++)
    {
        Game_Player *player = &game->players[i];
        Game_Entity *entity = &game->entities[player->entityIndex];
        entity->radius = 14;

        if(i == 0)
        {
            entity->x = 1.5*game->tilemap->image->width / game->tilemap->image->columns;
        }
        else if(i == 1)
        {
            entity->x = 18.5*game->tilemap->image->width / game->tilemap->image->columns;
        }

        entity->y = 8.5*game->tilemap->image->height / game->tilemap->image->rows;
        entity->deleteFlag = 0;
    }

    for(int i = 0; i < 8; i++)
    {
        Game_Entity *e = game_entity_spawn(game);
        e->radius = 8;
        e->x = 48 + (rand()%18) * 32.;
        e->y = 48 + (rand()%8) * 32.;
        e->sprite.image = gfx_image_get("game_fruits");
        e->sprite.frame = rand() % 30;
    }
}

int game_sample_logic(void *game, float ticks)
{
    Game *server = game;

    // Player-specific logic
    for(int i = 0; i < array_size(server->players); i++)
    {
        Game_Player *player = &server->players[i];
        if(player->inUse)
        {
            game_sample_input_player(server, player, ticks);
            Game_Entity *entity = &server->entities[player->entityIndex];

            player->camX = entity->x;
            player->camY = entity->y;

        }
    }

    return server->gameOver;
}

void game_sample_collide_entity(void *game, float ticks, Game_Entity *e1, Game_Entity *e2)
{
    // Default action is to push entities
    game_collide_reponse_entity_entity(game, ticks, e1, e2);
}

void game_sample_collide_tilemap(void *game, float ticks, Game_Entity *e, int xtile, int ytile)
{
    Game *server = game;
    int tile = gfx_tilemap_get_tile(server->tilemap, xtile, ytile);
    if(tile)
    {
        game_collide_reponse_entity_tilemap(game, ticks, e, xtile, ytile);
    }
}

void game_sample_draw_player_gui(void *game, int x, int y, int w, int h, int localPlayer)
{
    Game *server = game;

    Game_Player *player = &server->players[localPlayer];
    Game_Entity *entity = &server->entities[player->entityIndex];

    if(player->inPause)
    {
        int status = gui_menu_pause(&player->itemPause, y, h);
        if(status == 1)
        {
            player->inPause = 0;
            clock_start(server->time);
        }
        else if(status == 2)
        {
            clock_start(server->time);
            server->gameOver = 1;
        }
    }
}

void game_sample_input_player(Game *server, Game_Player *player, float ticks)
{
    if(player->inUse)
    {
        Game_Entity *entity = &server->entities[player->entityIndex];
        entity->dx = 0;
        entity->dy = 0;

        if(player->actions & PLAYER_UP)
        {
            entity->dy = -64;
            entity->baseAnim = 0;
        }
        if(player->actions & PLAYER_DOWN)
        {
            entity->dy = +64;
            entity->baseAnim = 18;
        }
        if(player->actions & PLAYER_LEFT)
        {
            entity->dx = -64;
            entity->baseAnim = 9;
        }
        if(player->actions & PLAYER_RIGHT)
        {
            entity->dx = +64;
            entity->baseAnim = 27;
        }

        // Player animation
        if(entity->dx == 0 && entity->dy == 0)
        {
            entity->animTime = 0;
            entity->sprite.frame = entity->baseAnim;
        }
        else
        {
            entity->animTime += ticks;
            entity->sprite.frame = ((int)(entity->animTime * 10) % 8) + entity->baseAnim + 1;
        }

        if(player->actions & PLAYER_START)
        {
            if(!server->time->isPaused)
            {
                player->inPause = 1;
                player->itemPause = 0;
                clock_pause(server->time);
            }
        }
    }
}

